package com.example.thirdhomework

import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.os.Handler
import android.provider.MediaStore
import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

const val First_MSG = "First_MSG"
const val Second_MSG = "Second_MSG"
class MainActivity : AppCompatActivity() {
    var seconds = 0
    var running = false
    var wasRunning = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        val first: Button = findViewById(R.id.first)
        first.setOnClickListener {
            val intent = Intent(this, SecondActivity::class.java)
            val editText: EditText = findViewById(R.id.editText)
            intent.putExtra(First_MSG, editText.text.toString())
            startActivity(intent)
        }

        val second: Button = findViewById(R.id.second)
        second.setOnClickListener {
            val intent = Intent(this, ThirdActivity::class.java)
            startActivityForResult(intent, 0)
        }

        val third: Button = findViewById(R.id.third)
        third.setOnClickListener {
            dispatchTakePictureIntent()
        }

        if (savedInstanceState != null) {
            seconds = savedInstanceState.getInt("seconds")
            running = savedInstanceState.getBoolean("running")
            wasRunning = savedInstanceState.getBoolean("wasRunning")
        }

        runTimer()

        val button_start: Button = findViewById(R.id.button_start)
        val button_stop: Button = findViewById(R.id.button_stop)
        val button_restart: Button = findViewById(R.id.button_restart)

        button_start.setOnClickListener {
            running = true
        }
        button_stop.setOnClickListener {
            running = false
        }
        button_restart.setOnClickListener {
            running = true
            seconds = 0
        }
    }
    fun runTimer() {
        val handler = Handler()
        val runnable = object: Runnable {
            override fun run() {
                val hours = seconds / 3600
                val minutes = (seconds % 3600) / 60
                val secs = seconds % 60
                val textView_timer: TextView= findViewById(R.id.textView_timer)
                textView_timer.text = String.format("%02d:%02d:%02d", hours, minutes, secs)
                if (running) {
                    seconds ++
                }
                handler.postDelayed(this,1000)
            }
        }
        handler.post(runnable)
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putInt("seconds", seconds)
        outState.putBoolean("running", running)
        outState.putBoolean("wasRunning", wasRunning)
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d("life cycle", "onDestroy")
    }
    override fun onStart() {
        super.onStart()
        Log.d("life cycle", "onStart")
    }
    override fun onStop() {
        super.onStop()
        Log.d("life cycle", "onStop")
    }
    override fun onResume() {
        super.onResume()
        Log.d("life cycle", "onResume")
        if (wasRunning) {
            running = true
        }
    }
    override fun onPause() {
        super.onPause()
        Log.d("life cycle", "onPause")
        wasRunning = running
        running = false
    }



    val REQUEST_IMAGE_CAPTURE = 1

    private fun dispatchTakePictureIntent() {
        val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        if(intent.resolveActivity(packageManager) != null){
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
        }
    }

    @Deprecated("This method has been deprecated in favor of using the Activity Result API\n      which brings increased type safety via an {@link ActivityResultContract} and the prebuilt\n      contracts for common intents available in\n      {@link androidx.activity.result.contract.ActivityResultContracts}, provides hooks for\n      testing, and allow receiving results in separate, testable classes independent from your\n      activity. Use\n      {@link #registerForActivityResult(ActivityResultContract, ActivityResultCallback)}\n      with the appropriate {@link ActivityResultContract} and handling the result in the\n      {@link ActivityResultCallback#onActivityResult(Object) callback}.")
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode == 0){
            if(resultCode == 1){
                val textView: TextView = findViewById(R.id.textView)
                textView.text = data?.getStringExtra(Second_MSG)
            }
        }
        if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK){
            val imageBitmap: Bitmap = data?.extras?.get("data") as Bitmap
            val imageView: ImageView = findViewById(R.id.imageView)
            imageView.setImageBitmap(imageBitmap)
        }
    }


}